package com.demo.webapp.kcfinder.config;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.demo.common.RegexUtils;

/**
 * ActionConfig
 * Action配置的基础对象
 * 提供Action的相关属性
 * 1.ActonName
 * 2.PackageName
 * 3.InterceptorMap
 * 4.ParamMap
 * 5.ResultConfig
 * 
 * @author ryuu.kk
 *
 */
public class ActionConfig implements Serializable {

	/**
	 * 版本序列化ID
	 */
	private static final long serialVersionUID = -8578844745014436762L;
	//默认方法名
	public static final String DEFAULT_METHOD = "execute";
	//通配符
    public static final String WILDCARD = "*";
    
	protected String actionClass;
	protected String name;
	protected String method;
	protected Map<String, String> params;
	protected Map<String, ResultConfig> resultMap;
	protected List<String> interceptors;
	
	public ActionConfig(String actionClass, String actionName, String methodName) {
		this.actionClass = actionClass;
		this.name = actionName;
		this.method = methodName;
		this.resultMap = new HashMap<String, ResultConfig>();
		this.params = new HashMap<String, String>();
		this.interceptors = new ArrayList<String>();
	}
	
	public ActionConfig() {
		this.resultMap = new HashMap<String, ResultConfig>();
		this.params = new HashMap<String, String>();
		this.interceptors = new ArrayList<String>();
	}
	
	public void addResultConfig(ResultConfig config) {
		if (resultMap.containsKey(config.getName())) {
			//throw new Exception("");
			//重复配置,存在异常
		}
		resultMap.put(config.getName(), config);
	}
	
	public void addInterceptor(String interceptorName) {
		interceptors.add(interceptorName);
	}
	
	public void addParams(String key, String value) {
		//简单实现
		params.put(key, value);
	}

	public String getActionClass() {
		return actionClass;
	}

	public void setActionClass(String actionClass) {
		this.actionClass = actionClass;
	}

	public void setName(String actionName) {
		this.name = actionName;
	}

	public void setMethod(String methodName) {
		this.method = methodName;
	}

	public String getName() {
		return name;
	}

	public String getMethod() {
		return (method != null && method.length() > 0) ? method : DEFAULT_METHOD;
	}

	public Map<String, String> getParams() {
		return params;
	}

	public Map<String, ResultConfig> getResultMap() {
		return resultMap;
	}
	
	public List<String> getInterceptors() {
		return interceptors;
	}

	public boolean support(String name, String method) {
		//判断name和method是否被支持,提供通配符验证方法
		return (RegexUtils.wildcardMatch(this.getName(), name) && (RegexUtils.wildcardMatch("{?}", this.getMethod()) ? true : this.getMethod().equals(method)));
	}
}